package no.ntnu.item.ttm4115.termproject.playeragent;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import javax.swing.JDialog;
import javax.swing.JOptionPane;

import com.thoughtworks.xstream.XStream;

import no.tellu.common.javaframe.ActorAddress;
import no.ntnu.item.ttm4115.termproject.RegisterPlayer;
import no.ntnu.item.ttm4115.termproject.extrasagent.Group;
import no.ntnu.item.ttm4115.termproject.extrasagent.Player;
import no.ntnu.item.ttm4115.termproject.playeragent.gui.NewGroupDialog;
import no.ntnu.item.ttm4115.termproject.playeragent.gui.PlayerGameGUI;
import no.ntnu.item.ttm4115.termproject.SetOnlinePlayers;
import no.ntnu.item.ttm4115.termproject.InitiateIMFromGUI;
import no.tellu.common.javaframe.messages.RoleRequestMsg;
import no.tellu.common.actorframe.messages.RoleConfirmMsg;
import no.tellu.common.actorframe.messages.RoleReleaseMsg;
import no.ntnu.item.ttm4115.termproject.InitiateIMCounterpart;
import no.ntnu.item.ttm4115.termproject.InitiateDiscussionFromGUI;
import no.tellu.common.actorframe.messages.RoleDeniedMsg;
import no.ntnu.item.ttm4115.termproject.InitiateGroupChatFromGUI;
import no.ntnu.item.ttm4115.termproject.InitiateNewGroupChat;

import no.ntnu.item.ttm4115.termproject.StartUpRoleGA;

import no.ntnu.item.ttm4115.termproject.StartedGroupChatToPlayers;
import no.ntnu.item.ttm4115.termproject.JoinDiscussion;
import no.ntnu.item.ttm4115.termproject.NewPlayerInGroupChat;
import no.ntnu.item.ttm4115.termproject.InformGroupAboutDiscussion;
import no.ntnu.item.ttm4115.termproject.ReleasePlayerGroupChatAgent;
import no.tellu.common.actorframe.messages.RoleRemoveMsg;
import no.ntnu.item.ttm4115.termproject.PlayerChangesStatusInGUI;
import no.ntnu.item.ttm4115.termproject.ForwardedClue;
import no.ntnu.item.ttm4115.termproject.CoordsFromGUI;
import no.ntnu.item.ttm4115.termproject.ForwardCoordsToGA;
import no.ntnu.item.ttm4115.termproject.GetPlayerNameForGameAgents;
import no.ntnu.item.ttm4115.termproject.PlayerNameToPlayerGameAgent;
import no.ntnu.item.ttm4115.termproject.RegisterCall4Help;
import no.ntnu.item.ttm4115.termproject.ReleasePlayerDiscussionAgent;
import no.ntnu.item.ttm4115.termproject.ImEstablishedToPa;
import no.ntnu.item.ttm4115.termproject.sendMyFrameToIM;
import no.ntnu.item.ttm4115.termproject.ReleaseThisImAgent;
import no.ntnu.item.ttm4115.termproject.UpdatePlayerNewStatusToAll;
import no.ntnu.item.ttm4115.termproject.SendGroupPlayersToPA;
import no.ntnu.item.ttm4115.termproject.Login;
import no.ntnu.item.ttm4115.termproject.GetMyMainFrame;
import no.ntnu.item.ttm4115.termproject.ReturnMainFrameFromPA;
import no.ntnu.item.ttm4115.termproject.UpdateStatusToPlayers;
import no.ntnu.item.ttm4115.termproject.ReturnPlayersForGroupCreation;
import no.ntnu.item.ttm4115.termproject.SendRoleRequestToNewGroup;
import no.ntnu.item.ttm4115.termproject.InvitePlayer;
import no.ntnu.item.ttm4115.termproject.Invitees;
import no.ntnu.item.ttm4115.termproject.GetGUIRefFromPlayerAgent;
import no.ntnu.item.ttm4115.termproject.GUIRefFromPlayerAgent;
import no.ntnu.item.ttm4115.termproject.PositionAgentAddressToPlayer;
import no.ntnu.item.ttm4115.termproject.PositionAgentAddressFromPlayerAgent;
import no.ntnu.item.ttm4115.termproject.RegisterPlayerPositionAgent;
import no.ntnu.item.ttm4115.termproject.GameEnd;
import no.ntnu.item.ttm4115.termproject.RegisterSmPa;
import no.ntnu.item.ttm4115.termproject.StartSmPa;
import no.ntnu.item.ttm4115.termproject.PlayerUpdateFromPA;
import no.ntnu.item.ttm4115.termproject.StartCallHelp;





public class PlayerAgentActions extends Object {

	@SuppressWarnings("unchecked")
	public static void publishOnlinePlayers(SetOnlinePlayers signal, PlayerAgentSM asm){
	    
		XStream xstream = new XStream();
		xstream.setClassLoader(new Player().getClass().getClassLoader());
		xstream.alias("player", Player.class);
		xstream.alias("group", Group.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
		
		Object object = (Object)xstream.fromXML(signal.xmlUserHashMap);
		Player player = (Player)object;
		System.out.println("Du mottok spiller: "+player.getUserName());
		
		if(!asm.onlinePlayersMap.containsKey(player.getUserName())){
			asm.onlinePlayersMap.put(player.getUserName(), player);
		}
		
		if(player.getUserName().equals(asm.player.getUserName())){
				asm.player.setLocation(player.getLocation());
				asm.loginDialog.getGuiRef().setLocation(asm.player.getLocation());
		}
		asm.myClassManagerAddress = signal.getSenderRole();
		
		asm.loginDialog.getGuiRef().updateOnlinePlayersList(asm.onlinePlayersMap);
		
	}

	public static void sendRoleRequestToIM(InitiateIMFromGUI signal, PlayerAgentSM asm){
	    
		asm.imName = signal.imName;
		asm.mainFrame = signal.mainFrame;
		
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		String time = formatter.format(date);
		asm.loginDialog.getGuiRef().updateActivityList(time+": Initiated a IM chat with "+asm.imName, "IM");
		asm.sendMessage(new RoleRequestMsg(asm.imName+"/imAgent", "IMAgent"), asm.getMyActorAddress());
	}

	@SuppressWarnings("unchecked")
	public static void confirmAck(RoleConfirmMsg signal, PlayerAgentSM asm){
	    
		asm.myIMAgentAddress = signal.getSenderRole();
		
		Iterator<Map.Entry> playerit = asm.onlinePlayersMap.entrySet().iterator();
		
		while(playerit.hasNext()){
			Map.Entry me = (Map.Entry)playerit.next();
			
			Player player = (Player)me.getValue();
			
			if (asm.imName.equals(player.getUserName())){
				asm.recevierIMAgentAddress = player.getActorAddress();			
			
			}
		}
		
		
	asm.sendMessage(new InitiateIMCounterpart(asm.myName, asm.imName, asm.recevierIMAgentAddress, asm.mainFrame), signal.getSenderRole());
	}


	public static void SendRoleRequestDiscussionAgent(InitiateDiscussionFromGUI signal, PlayerAgentSM asm){
		asm.myGroupName = signal.groupName;
		asm.myGroupAddress = signal.groupAddress;
		asm.mainFrame = signal.mainFrame;
		
		if(!asm.myGroupDisStatus.containsKey(signal.groupName)){
			asm.startedGroupDisTemp = 0;				
		}
		else{
			asm.startedGroupDisTemp = 1;
			asm.myDiscussionAgentAdress = (ActorAddress)asm.myGroupDisStatus.get(signal.groupName);
		}
		
	    asm.sendMessage(new RoleRequestMsg(asm.myName+"/"+asm.myGroupName+"/discussionAgent", "DiscussionAgent"), asm.getMyActorAddress());
		
	}

	public static void confirmAckToGCA(RoleConfirmMsg signal, PlayerAgentSM asm){
	   
		System.out.println("Player fikk role confirm fra gruppa");
		asm.myGroupChatAddress = signal.getSenderRole();
	}



	public static void discussionDeniedHandle(RoleDeniedMsg signal, PlayerAgentSM asm){
	    System.out.println("mottatt roledeny fra "+signal.getSenderRole());
	}

	public static void gcaRoleDenied(RoleDeniedMsg signal, PlayerAgentSM asm){
	    
		System.out.println("GroupChatDenied");
	}



	public static void returnAckForDiscussion(RoleConfirmMsg signal, PlayerAgentSM asm){
		
		System.out.println("Player fikk role confirm fra gruppa");
		asm.myDiscussionAgentAdress =signal.getSenderRole();
	    
	}

	public static boolean NoCurrentDiscussion(RoleConfirmMsg signal, PlayerAgentSM asm){
		
	    if(asm.startedGroupDisTemp == 0){
			return true;
		}
		
	    return false;
	}

	public static void sendRoleRequestToGCA(InitiateGroupChatFromGUI signal, PlayerAgentSM asm){
		asm.myGroupName = signal.groupName;
		asm.myGroupAddress = signal.groupAddress;
		asm.mainFrame = signal.mainFrame;
		
		if(!asm.myGroupsChatStatus.containsKey(signal.groupName)){
			asm.startedGroupChatTemp = 0;				
		}
		else{
			asm.startedGroupChatTemp = 1;
			asm.myGroupChatAddress = (ActorAddress)asm.myGroupsChatStatus.get(signal.groupName);
		}
		asm.sendMessage(new RoleRequestMsg(asm.myName+"/"+signal.groupName+"/groupChatAgent", "GroupChatAgent"), asm.getMyActorAddress());
			
	}

	public static boolean notExcistingChat(RoleConfirmMsg signal, PlayerAgentSM asm){
		
		
		if(asm.startedGroupChatTemp == 0){
			return true;
		}
		
	    return false;
	}

	public static void MakeDiscussionAgentRRGroup(PlayerAgentSM asm){
	    System.out.println("No discussion started");
	    
	    asm.sendMessage(new StartUpRoleGA(asm.myGroupAddress, asm.myName, asm.myGroupName, asm.mainFrame),asm.myDiscussionAgentAdress);
	}

	@SuppressWarnings("unchecked")
	public static void informSomeoneStartedGC(StartedGroupChatToPlayers signal, PlayerAgentSM asm){
	   
		asm.groupsChatAgentAddress = signal.GCAGAddress;
		if(!asm.myGroupsChatStatus.containsKey(signal.groupName)){
			asm.myGroupsChatStatus.put(signal.groupName, signal.GCAGAddress);
		}
		
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		String time = formatter.format(date);
		String message = time+": "+signal.groupName+" started a group chat";
		asm.loginDialog.getGuiRef().updateActivityList(message, "Chat");
		
	}



	public static void informUserOfDenial(RoleDeniedMsg signal, PlayerAgentSM asm){

		JOptionPane.showMessageDialog(asm.loginDialog.getGuiRef(), "Please finish another IM to start a new one!");
	}

	public static void joinGChat(PlayerAgentSM asm){

		System.out.println("Chat already started. Joining..");
		
		asm.sendMessage(new NewPlayerInGroupChat(asm.myName, asm.myGroupChatAddress, asm.myGroupName), asm.groupsChatAgentAddress);

	}

	@SuppressWarnings("unchecked")
	public static void discussionStarted(InformGroupAboutDiscussion signal, PlayerAgentSM asm){
		
		asm.groupDiscussionAgentAddress = signal.groupDiscussionAgentAddress;
		if(!asm.myGroupDisStatus.containsKey(signal.groupName)){
			asm.myGroupDisStatus.put(signal.groupName, signal.groupDiscussionAgentAddress);
		}
		
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		String time = formatter.format(date);
		String message = time+": "+signal.groupName+" started a group discussion";
		asm.loginDialog.getGuiRef().updateActivityList(message, "Discussion");
	}

	public static void joinCurrentDiscussion(PlayerAgentSM asm){
		asm.sendMessage(new JoinDiscussion(asm.myName, asm.myDiscussionAgentAdress, asm.myGroupName), asm.groupDiscussionAgentAddress);
	}

	public static void releaseMyGroupChatAgent(ReleasePlayerGroupChatAgent signal, PlayerAgentSM asm){

		asm.sendMessage(new RoleReleaseMsg(), signal.getSenderRole());
		asm.sendMessage(new RoleRemoveMsg(), signal.getSenderRole());
	}

	public static void printClue(ForwardedClue signal, PlayerAgentSM asm){
		asm.groupLeaderTemp = signal.groupLeader;
		if(!(asm.gamePanelGUI == null)){
			if(!asm.gamePanelGUI.getThisGameName().equals(signal.gameName)){
				asm.gamePanelGUI = new PlayerGameGUI(asm, asm.mainFrame,signal.gameName, asm.playerPositionAgentAddress, signal.groupLeader);
				
			}
			else{
				asm.gamePanelGUI.setClueText(signal.clue);
			}

		}
		else{
			asm.gamePanelGUI = new PlayerGameGUI(asm, asm.mainFrame,signal.gameName, asm.playerPositionAgentAddress, signal.groupLeader);
			Date date = new Date();
			SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
			String time = formatter.format(date);
			String message = time+": "+signal.gameName+" has been initiated";
			asm.loginDialog.getGuiRef().updateActivityList(message, "System");
		}
		asm.gamePanelGUI.setClueText(signal.clue);
	}

	public static void releaseMyDiscussionAgent(ReleasePlayerDiscussionAgent signal, PlayerAgentSM asm){
		asm.sendMessage(new RoleReleaseMsg(), signal.getSenderRole());
	}

	public static void forwardCoordsFromGUI(CoordsFromGUI signal, PlayerAgentSM asm){
		System.out.println(signal.Xcord);
		System.out.println(signal.Ycord);
		asm.sendMessage(new ForwardCoordsToGA(signal.Xcord,signal.Ycord), asm.myGroupAddress);
	}
	public static void sendMyFrame(ImEstablishedToPa signal, PlayerAgentSM asm){

		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		String time = formatter.format(date);
		
		asm.loginDialog.getGuiRef().updateActivityList(time+": "+signal.imChatWithName+" requested IM chat", "IM");
		asm.mainFrame = asm.loginDialog.getGuiRef();
		asm.sendMessage(new sendMyFrameToIM(asm.mainFrame), signal.getSenderRole());
	}

	public static void sendRoleRemoveToIm(ReleaseThisImAgent signal, PlayerAgentSM asm){

			asm.sendMessage(new RoleReleaseMsg(), signal.getSenderRole());
			asm.sendMessage(new RoleRemoveMsg(), signal.getSenderRole());
	
	}

	public static void changePlayerStatus(PlayerChangesStatusInGUI signal, PlayerAgentSM asm){

		asm.player.setStatus(signal.newStatus);
		
		XStream xstream = new XStream();
		xstream.setClassLoader(new Player().getClass().getClassLoader());
		xstream.alias("player", Player.class);
		xstream.alias("group", Group.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
		
		String statusChangePlayer = xstream.toXML(asm.player);
		
		asm.sendMessage(new UpdatePlayerNewStatusToAll(statusChangePlayer), new ActorAddress("/classManager", "ClassManager"));
	
	}



	public static void sendNameToPlayerGameAgent(GetPlayerNameForGameAgents signal, PlayerAgentSM asm){
	    // 
	asm.sendMessage(new PlayerNameToPlayerGameAgent(asm.myName), signal.getSenderRole());
	}

	@SuppressWarnings({ "unchecked", "unchecked" })
	public static void hashMyGroupsMembers(SendGroupPlayersToPA signal, PlayerAgentSM asm){
	   
		XStream xstream = new XStream();
		xstream.setClassLoader(new Player().getClass().getClassLoader());
		xstream.alias("player", Player.class);
		xstream.alias("group", Group.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
		
		Object object = xstream.fromXML(signal.xmlGroupPlayersMap);
		HashMap groupsPlayersMap = (HashMap)object;
	
		System.out.println("Mottok signalet her! ");
		System.out.println("fra "+signal.getSenderRole());
		System.out.println("obj:..."+groupsPlayersMap.toString());
		 if(!asm.myGroupsPlayers.containsKey(signal.groupName)){
	    	asm.myGroupsPlayers.put(signal.groupName, groupsPlayersMap);
	    }
		
		asm.loginDialog.getGuiRef().setMyGroupsMap(signal.groupName, groupsPlayersMap);
	}

	public static void initialize(PlayerAgentSM asm){

		System.out.println("Player agent: "+asm.myActorId +" is initialized here..NOT");
		//asm.sendMessage(new RegisterPA(asm.getMyActorAddress()), new ActorAddress("/classManager","ClassManager"));
	}

	

	public static void login(Login signal, PlayerAgentSM asm){
	        
			asm.loginDialog.loginSuccessful(signal);
			XStream xstream = new XStream();
			xstream.setClassLoader(new Player().getClass().getClassLoader());
			xstream.alias("player", Player.class);
			xstream.alias("group", Group.class);
			xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
			
			Object object = xstream.fromXML(signal.playerXML);
			Player player = (Player)object;
			
			asm.player = player;
			asm.myName = player.getUserName();
			
					
			Date date = new Date();
			SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
			String time = formatter.format(date);
			String loggedin = time+": You logged in";
			
			
			asm.loginDialog.getGuiRef().updateActivityList(loggedin, "System");
			asm.loginDialog.getGuiRef().updateMyGroupsList(player);
			asm.loginDialog.getGuiRef().setPlayerName(player.getUserName());
			//asm.loginDialog.getGuiRef().setLocation(player.getLocation());
			
	
			
			String xmlString = xstream.toXML(asm.player);
			
			
			for (int i=0; i<player.getGroups().size(); i++){
				System.out.println("Grupper: "+player.getGroups().get(i));
				asm.sendMessage(new RegisterPlayer(xmlString, player.getGroups().get(i).getGroupName()), player.getGroups().get(i).getActorAddress());
			}
			asm.sendMessage(new RegisterPlayer(xmlString, "classManager"), new ActorAddress("/classManager", "ClassManager"));

	}

	public static void initiateGCCounterpart(PlayerAgentSM asm){
	    System.out.println("No chat started. Initiating..");
				asm.sendMessage(new InitiateNewGroupChat(asm.myGroupAddress, asm.myGroupName, asm.myName, asm.mainFrame), asm.myGroupChatAddress);
	}

	public static void sendMyMainFrame(GetMyMainFrame signal, PlayerAgentSM asm){
	    asm.mainFrame = asm.loginDialog.getGuiRef();
	    asm.sendMessage(new ReturnMainFrameFromPA(asm.mainFrame), signal.getSenderRole());
	}

	public static void publishStatusUpdateFromPlayer(UpdateStatusToPlayers signal, PlayerAgentSM asm){
		XStream xstream = new XStream();
		xstream.setClassLoader(new Player().getClass().getClassLoader());
		xstream.alias("player", Player.class);
		xstream.alias("group", Group.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
			
		Object object = (Object)xstream.fromXML(signal.xmlPlayer);
		Player player = (Player)object;
		System.out.println("Du mottok spiller: "+player.getUserName());
		
		if(asm.onlinePlayersMap.containsKey(player.getUserName())){
			asm.onlinePlayersMap.put(player.getUserName(), player);
		}
		
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		String time = formatter.format(date);
		
		asm.loginDialog.getGuiRef().updateOnlinePlayersList(asm.onlinePlayersMap);
	
		if(!asm.player.getUserName().equals(player.getUserName())){
			asm.loginDialog.getGuiRef().updateActivityList(time+": "+player.getUserName()+" changed status to "+player.getStatus(), "Status");
		}
		// make call to update
		System.out.println("Sending update to: " + asm.getMyActorAddress().getActorID()+"/socialMatchingRole_PlayerAgent");
		asm.sendMessage(signal,new ActorAddress(asm.getMyActorAddress().getActorID()+"/socialMatchingRole_PlayerAgent","SocialMatchingRole_PlayerAgent"));
	}

	public static void PrepareNewGroupGUI(ReturnPlayersForGroupCreation signal, PlayerAgentSM asm){
		XStream xstream = new XStream();
		xstream.setClassLoader(new Player().getClass().getClassLoader());
		xstream.alias("player", Player.class);
		xstream.alias("group", Group.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
		
		Object object = (Object)xstream.fromXML(signal.xmlPlayer);
		Player player = (Player)object;
		asm.playerConfig.add(player);
		
		if(signal.playerFinished){
			NewGroupDialog newGroup = new NewGroupDialog(asm, asm.playerConfig, asm.player, asm.myClassManagerAddress);
			newGroup.setVisible(true);
			asm.playerConfig.removeAllElements();
		}
		
		
	}

	public static void sendRoleRequestToGroup(SendRoleRequestToNewGroup signal, PlayerAgentSM asm){
	    XStream xstream = new XStream();
		xstream.setClassLoader(new Player().getClass().getClassLoader());
		xstream.alias("player", Player.class);
		xstream.alias("group", Group.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
			
		Object object = (Object)xstream.fromXML(signal.xmlPlayer);
		Player player = (Player)object;
		asm.tempGroupName = signal.groupName;
		asm.player = player;

		asm.sendMessage(new RoleRequestMsg(signal.groupName, "GroupAgent"), asm.myClassManagerAddress);
	}

	public static void unsuccessfulGroupCreation(RoleDeniedMsg signal, PlayerAgentSM asm){
		Date date = new Date();
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		String time = formatter.format(date);
		asm.loginDialog.getGuiRef().updateActivityList(time+": Creation of "+asm.tempGroupName+" failed", "System");
	}

	public static void publishGroupInvite(InvitePlayer signal, PlayerAgentSM asm){
	    // nothing to do here
	}

	public static boolean AcceptedInvite(InvitePlayer signal, PlayerAgentSM asm){
		XStream xstream = new XStream();
		xstream.setClassLoader(new Player().getClass().getClassLoader());
		xstream.alias("player", Player.class);
		xstream.alias("group", Group.class);
		xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
		
		Object object = xstream.fromXML(signal.group);
		Group group = (Group)object;
	
		JDialog.setDefaultLookAndFeelDecorated(true);
        int response = JOptionPane.showConfirmDialog(null,signal.invitee+" sent you a invitiation to join "+group.getGroupName()+"\n"+"Do you accept the invitation? ", "Group Invitation",
            JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE);
        if (response == JOptionPane.NO_OPTION) {
          return false;
        } else if (response == JOptionPane.YES_OPTION) {
        	return true;
          
        } else if (response == JOptionPane.CLOSED_OPTION) {
        	return false;
        }
	    return false;
	}

	public static void notAccepted(PlayerAgentSM asm){
	    // TODO edit action statements
		System.out.println("Feil side");
		
	}

	public static void storeMapForInvitees(Invitees signal, PlayerAgentSM asm){
	
		
		Iterator<Map.Entry> iterator = asm.onlinePlayersMap.entrySet().iterator();
		
		while(iterator.hasNext()){
			Map.Entry<String, Player> me = (Map.Entry<String, Player>)iterator.next();
			for(int i=0; i<signal.groupPlayersVector.size(); i++){
				if(me.getKey().toString().equals(signal.groupPlayersVector.get(i))){
					asm.tempHashMapInvitees.put(me.getKey(), me.getValue().getActorAddress());
					System.out.println("putta inn "+me.getKey());
				}
			}
			
		}
		
		
	}

	public static void sendGUIRefToGameAgent(GetGUIRefFromPlayerAgent signal, PlayerAgentSM asm){
		
	asm.sendMessage(new GUIRefFromPlayerAgent(asm.gamePanelGUI, asm.groupLeaderTemp), signal.getSenderRole());
	}

	public static void registerAtGroup(InvitePlayer signal, PlayerAgentSM asm){
			XStream xstream = new XStream();
			xstream.setClassLoader(new Player().getClass().getClassLoader());
			xstream.alias("player", Player.class);
			xstream.alias("group", Group.class);
			xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
			
			Object object = xstream.fromXML(signal.group);
			Group group = (Group)object;
		
			asm.player.addGroup(group);
			asm.loginDialog.getGuiRef().updateMyGroupsList(asm.player);
			String xmlPlayer = xstream.toXML(asm.player);
		
			asm.sendMessage(new RegisterPlayer(xmlPlayer, group.getGroupName()), group.getActorAddress());
	}

	public static void startUpPlayerPositionAgent(PositionAgentAddressToPlayer signal, PlayerAgentSM asm){
		asm.PositionAgentAddress = signal.PositionAgentAddress;
		String pos = "PlayerPositionAgent"+(int)((Math.random()*1000)+1);
		asm.sendMessage(new RoleRequestMsg(pos,"PlayerPositionAgent"),asm.getMyActorAddress() );
	}

	public static boolean gameDoesntExist(ForwardedClue signal, PlayerAgentSM asm){
		Iterator<Map.Entry> iterator = asm.onGoingGames.entrySet().iterator();
		if(asm.onGoingGames.containsKey(signal.gameName)){
			return false;
		}
		return true;
	}
	public static void useExistingGameGUI(ForwardedClue signal,PlayerAgentSM asm){
	    System.out.println("Bruker gammel gui");
	    
	    asm.gamePanelGUI.setClueText(signal.clue);
	    
	    // remove treasure tabbedpane
	    asm.gamePanelGUI.getGameTabbePane().remove(1);
	    
/*	    Iterator<Map.Entry> iterator = asm.onGoingGames.entrySet().iterator();
		while(iterator.hasNext()){
			Map.Entry<String,PlayerGameGUI> me = (Map.Entry<String, PlayerGameGUI>)iterator.next();
			if(signal.gameName ==me.getKey()){
				me.getValue().setClueText(signal.clue);
				System.out.println("...Player"+asm.getMyActorAddress()+" .. get new clue: "+signal.clue);
				
			}
		}*/
	}

	public static void sendPositionAgentAddress(RoleConfirmMsg signal, PlayerAgentSM asm){
	    asm.playerPositionAgentAddress = signal.getSenderRole();
		asm.sendMessage(new PositionAgentAddressFromPlayerAgent(asm.PositionAgentAddress, asm.myGroupAddress,asm.myName), signal.getSenderRole());
	}

	public static void startNewGameGUI(ForwardedClue signal, PlayerAgentSM asm){
	    asm.myGroupAddress = signal.getSenderRole();
			asm.mainFrame = asm.loginDialog.getGuiRef();
			System.out.println("Lager ny gui");
			System.out.println(asm.playerPositionAgentAddress.toString());
			asm.gamePanelGUI = new PlayerGameGUI(asm, asm.mainFrame,signal.gameName, asm.playerPositionAgentAddress, signal.groupLeader);
			Date date = new Date();
			SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
			String time = formatter.format(date);
			String message = time+": "+signal.gameName+" has been initiated";
			asm.loginDialog.getGuiRef().updateActivityList(message, "System");
			asm.onGoingGames.put(signal.gameName, asm.gamePanelGUI);
			asm.gamePanelGUI.setClueText(signal.clue);
			
			asm.sendMessage(new RegisterPlayerPositionAgent(asm.playerPositionAgentAddress,asm.myName), signal.getSenderRole());
	}

	public static void gameEnded(GameEnd signal, PlayerAgentSM asm){
		
		// update GUI
		asm.gamePanelGUI.getGameTabbePane().remove(1);
		asm.gamePanelGUI.gameEnded("Congratulations !! Game Started at "+signal.startTimeStamp+" and Ended at "+signal.endTimeStamp);
		
	}

	public static void registerSmPa(RegisterSmPa signal, PlayerAgentSM asm){
		asm.sendMessage(new RoleRequestMsg("socialMatchingRole_PlayerAgent","SocialMatchingRole_PlayerAgent"),asm.getMyActorAddress());
	}

	public static void startSM(RoleConfirmMsg signal, PlayerAgentSM asm){
		asm.sendMessage(new StartSmPa(asm.loginDialog.getGuiRef()), signal.getSenderRole());
	}

	public static void successfullyCreatedGroup(RoleConfirmMsg signal, PlayerAgentSM asm){
	    XStream xstream = new XStream();
			xstream.setClassLoader(new Player().getClass().getClassLoader());
			xstream.alias("player", Player.class);
			xstream.alias("group", Group.class);
			xstream.setMode(XStream.XPATH_ABSOLUTE_REFERENCES);
					
					String xmlPlayer = xstream.toXML(asm.player);
					
					asm.loginDialog.getGuiRef().updateMyGroupsList(asm.player);
					Date date = new Date();
					
					for(int i=0; i<asm.player.getGroups().size(); i++){
						if(asm.player.getGroups().get(i).getGroupName().equals(asm.tempGroupName)){
							asm.player.getGroups().get(i).setActorAddress(signal.getSenderRole());
							asm.tempGroupCreated = xstream.toXML(asm.player.getGroups().get(i));
							asm.loginDialog.getGuiRef().setPlayer(asm.player);
						}
					}
					
					
					
					SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
					String time = formatter.format(date);
					asm.loginDialog.getGuiRef().updateActivityList(time+": Successfully created group "+asm.tempGroupName, "System");
					asm.sendMessage(new RegisterPlayer(xmlPlayer, asm.tempGroupName), signal.getSenderRole());
		
					
					Iterator<Map.Entry> iterator = asm.tempHashMapInvitees.entrySet().iterator();
					
					while(iterator.hasNext()){
						Map.Entry<String, ActorAddress> me = (Map.Entry<String, ActorAddress>)iterator.next();
						
						asm.sendMessage(new InvitePlayer(asm.player.getUserName(), asm.tempGroupCreated, signal.getSenderRole()), me.getValue());
						
					}
	
					asm.sendMessage(new PlayerUpdateFromPA(xmlPlayer), new ActorAddress(asm.myActorId+"/socialMatchingRole_PlayerAgent","SocialMatchingRole_PlayerAgent"));
	}

	public static void registerCall4Help(RegisterCall4Help signal, PlayerAgentSM asm){
		asm.sendMessage(new RoleRequestMsg("callhelpuserrole_PlayerAgent","CallhelpRole_PlayerAgent"),asm.getMyActorAddress());
	}

	public static void startCall4Help(RoleConfirmMsg signal, PlayerAgentSM asm){
		asm.sendMessage(new StartCallHelp(asm.loginDialog.getGuiRef()), signal.getSenderRole());
	}


	



}
